package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.bean.ScoreModel;
import com.shd.edanpei.presenter.contract.WithdrawContract;
import com.shd.edanpei.presenter.WithdrawPresenter;
import com.shd.edanpei.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/4/4.
 * 提现
 */
public class WithdrawActivity extends BaseActivity<WithdrawPresenter>
        implements WithdrawContract.WithdrawIView {

    public static WithdrawActivity instance;

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.tv_msg1)
    TextView tvMsg1;
    @BindView(R.id.tv_msg2)
    TextView tvMsg2;
    @BindView(R.id.tv_msg3)
    TextView tvMsg3;
    @BindView(R.id.et_withdraw_amount)
    EditText etAmount;
    @BindView(R.id.rg_withdraw)
    RadioGroup radioGroup;


    private long mTotal;
    private long mRatio;
    private boolean isOnclick;//设置点击一次

    @Override
    protected int setLayout() {
        return R.layout.withdraw_activity;
    }

    @Override
    protected WithdrawPresenter bindPresenter() {
        return new WithdrawPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        instance = this;
        headline.setText("提现");
        mPresenter.getData();
    }

    @Override
    protected void initListener() {
    }

    @OnClick(R.id.tv_submit)
    void withdraw() {
        if (TextUtils.isEmpty(etAmount.getText().toString())) {
            ToastUtil.showToast("请输入提现金额");
            return;
        }
        if (Integer.parseInt(etAmount.getText().toString()) < 100) {
            ToastUtil.showToast("提现金额最低100元");
            return;
        }
        if (Integer.parseInt(etAmount.getText().toString()) > mTotal / mRatio) {
            ToastUtil.showToast("金额已超过可提现金额");
            return;
        }
        if(isOnclick)
            return;

        Bundle bundle = new Bundle();
        bundle.putString(Constant.AMOUNT, etAmount.getText().toString());
        switch (radioGroup.getCheckedRadioButtonId()) {
            case R.id.rb_to_alipay:
                //提现到支付宝
                startActivity(WithdrawalToAlipay.class, bundle);
                isOnclick=true;
                break;
            case R.id.rb_to_bank:
                //提现到银行卡
                isOnclick=true;
                startActivity(WithdrawalToBank.class, bundle);
                break;
        }
    }

    @OnClick(R.id.iv_headline_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        isOnclick=false;
    }

    @Override
    public void setData(ScoreModel model) {
        tvMsg1.setText(model.getMsg1());
        tvMsg2.setText(model.getMsg2());
        tvMsg3.setText(model.getMsg3());
        mTotal = model.getPoints();
        mRatio = model.getRatio();
    }
}
